﻿using ConfExpert.Forms.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using System.Data;
using Extensions;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace ConfExpert.Utility.Grid
{
    public partial class ColumnFocuser : XPopupForm
    {
        private GridView GridView;

        public ColumnFocuser(GridView GridView)
        {
            InitializeComponent();


            this.GridView = GridView;
            DataTable dt = new DataTable("Columns");
            dt.Columns.Add("Sütun Adı", typeof(string));
            dt.Columns.Add("Index", typeof(int));

            foreach (GridColumn col in GridView.VisibleColumns)
            {
                DataRow dr = dt.NewRow();
                dr["Sütun Adı"] = (col.Caption == "" ? col.FieldName : col.Caption);
                dr["Index"] = col.AbsoluteIndex;
                dt.Rows.Add(dr);
            }

            gvColumnFocuser.Init(dt);
            gvColumnFocuser.MakeReadOnly();
            gvColumnFocuser.MakeRowSelectable();
            gvColumnFocuser.DoubleClick += gv_DoubleClick;
            gvColumnFocuser.MakeColumnInvisible("Index");
            gvColumnFocuser.BestFitColumns();



            gvColumnFocuser.FocusedRowHandle = DevExpress.XtraGrid.GridControl.AutoFilterRowHandle;

            gvColumnFocuser.KeyDown += gv_KeyDown;
            FooterText = "Gitmek istediğiniz sütuna çift tıklayınız.";
         
            //this.Text = GridView.Name;
        }

        void gv_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.KeyCode == Keys.Enter && gvColumnFocuser.DataRowCount == 1)
            if (e.KeyCode == Keys.Enter)
            {
                SelectedColumnIndex = (int)gvColumnFocuser.GetFocusedRowCellValue("Index");

                if (gvColumnFocuser.RowCount == 1)
                {

                    SelectedColumnIndex = (int)gvColumnFocuser.GetRowCellValue(0, "Index");
                    e.Handled = true;
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;                
                }
                else if (gvColumnFocuser.FocusedRowHandle > -1)
                {
                    SelectedColumnIndex = (int)gvColumnFocuser.GetFocusedRowCellValue("Index");
                    e.Handled = true;
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;                
                }
            }
            else if (e.KeyCode == Keys.Escape)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            }
            else
            {
                e.Handled = false;
            }
        }

        void gv_DoubleClick(object sender, System.EventArgs e)
        {
            Point pt = gc.PointToClient(Control.MousePosition);            
             GridHitInfo info = gvColumnFocuser.CalcHitInfo(pt);
             if (info.RowHandle > -1)
             {
                 SelectedColumnIndex = (int)gvColumnFocuser.GetFocusedRowCellValue("Index");
                 this.DialogResult = System.Windows.Forms.DialogResult.OK;
             }
        }

        public int SelectedColumnIndex { get; set; }

        private void ColumnFocuser_Load(object sender, System.EventArgs e)
        {
            ShowOnlyFooter();
        }
 
 
    }
}
